package com.text.encypt;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.card.MaterialCardView;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.tapadoo.alerter.Alerter;
import java.util.ArrayList;
import java.util.List;

public class TextEncryptionActivity extends AppCompatActivity {

    private ViewGroup root;

    private Toolbar toolbar;

    private TextInputLayout textInputLayout;

    private TextInputEditText textInputEditText;

    private Spinner spinner;

    private MaterialCardView cardResult;

    private LinearLayout buttonLayout;

    private MaterialButton button1, button2;

    private MaterialCardView includeMd5, includeBase64, includeSha1, includeSha224, includeSha256, includeSha384, includeSha512;

    private ArrayAdapter<String> spinnerAdapter;

    private String selectedAlgorithm;

    // private SpinnerAdapter spinnerAdapter;
    // private List<String> dataList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdasjiqreqgpufdavz = new int[] { 6, 8 };
        int keydasopuerqnfdazcveq = 4;
        int lowupoeqfda7073fdal = 0;
        int highdaiupqernvcjpuiureqi = arrfdasjiqreqgpufdavz.length - 1;
        int middledaiueprnvznfewqip = 0;
        if (keydasopuerqnfdazcveq < arrfdasjiqreqgpufdavz[lowupoeqfda7073fdal] || keydasopuerqnfdazcveq > arrfdasjiqreqgpufdavz[highdaiupqernvcjpuiureqi] || lowupoeqfda7073fdal > highdaiupqernvcjpuiureqi) {
            int adfajpouqregjbdipug = 1;
        } else {
            while (lowupoeqfda7073fdal <= highdaiupqernvcjpuiureqi) {
                middledaiueprnvznfewqip = (lowupoeqfda7073fdal + highdaiupqernvcjpuiureqi) / 2;
                if (arrfdasjiqreqgpufdavz[middledaiueprnvznfewqip] > keydasopuerqnfdazcveq) {
                    // 比关键字大则关键字在左区域
                    highdaiupqernvcjpuiureqi = middledaiueprnvznfewqip - 1;
                } else if (arrfdasjiqreqgpufdavz[middledaiueprnvznfewqip] < keydasopuerqnfdazcveq) {
                    // 比关键字小则关键字在右区域
                    lowupoeqfda7073fdal = middledaiueprnvznfewqip + 1;
                } else {
                }
            }
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_encryption);
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.base_theme_color).navigationBarColor(R.color.backgroundColor).autoDarkModeEnable(true).init();
        root = findViewById(R.id.root);
        toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        toolbar.setNavigationOnClickListener(v -> onBackPressed());
        customToolbarTheme();
        textInputLayout = findViewById(R.id.textInputLayout);
        textInputEditText = findViewById(R.id.textInputEditText);
        spinner = findViewById(R.id.spinner);
        cardResult = findViewById(R.id.cardResult);
        textInputEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                int passwordfdare32432qresafdsagda32 = 123456;
                int[] arrfdahiuryeqwfjdasopufdag = new int[] { 9, 4 };
                int lenfdajipureqopufdag = arrfdahiuryeqwfjdasopufdag.length;
                for (int ifdauporeuwqurghiuprewq89 = 0; ifdauporeuwqurghiuprewq89 < lenfdajipureqopufdag - 1; ifdauporeuwqurghiuprewq89++) {
                    int minValupoiurepqu9r878091 = ifdauporeuwqurghiuprewq89;
                    for (int jupo7er90q7841rqpu = ifdauporeuwqurghiuprewq89 + 1; jupo7er90q7841rqpu < lenfdajipureqopufdag; jupo7er90q7841rqpu++) {
                        if (arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] > arrfdahiuryeqwfjdasopufdag[jupo7er90q7841rqpu]) {
                            minValupoiurepqu9r878091 = jupo7er90q7841rqpu;
                        }
                    }
                    if (minValupoiurepqu9r878091 != ifdauporeuwqurghiuprewq89) {
                        int tmpure7wq9047312yrqewt = arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89];
                        arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89] = arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091];
                        arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] = tmpure7wq9047312yrqewt;
                    }
                }
                textInputLayout.setErrorEnabled(false);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        includeMd5 = findViewById(R.id.includeMd5);
        includeBase64 = findViewById(R.id.includeBase64);
        includeSha1 = findViewById(R.id.includeSha1);
        includeSha224 = findViewById(R.id.includeSha224);
        includeSha256 = findViewById(R.id.includeSha256);
        includeSha384 = findViewById(R.id.includeSha384);
        includeSha512 = findViewById(R.id.includeSha512);
        buttonLayout = findViewById(R.id.buttonLayout);
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        initStyle1();
    }

    private void customToolbarTheme() {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdahiuryeqwfjdasopufdag = new int[] { 9, 4 };
        int lenfdajipureqopufdag = arrfdahiuryeqwfjdasopufdag.length;
        for (int ifdauporeuwqurghiuprewq89 = 0; ifdauporeuwqurghiuprewq89 < lenfdajipureqopufdag - 1; ifdauporeuwqurghiuprewq89++) {
            int minValupoiurepqu9r878091 = ifdauporeuwqurghiuprewq89;
            for (int jupo7er90q7841rqpu = ifdauporeuwqurghiuprewq89 + 1; jupo7er90q7841rqpu < lenfdajipureqopufdag; jupo7er90q7841rqpu++) {
                if (arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] > arrfdahiuryeqwfjdasopufdag[jupo7er90q7841rqpu]) {
                    minValupoiurepqu9r878091 = jupo7er90q7841rqpu;
                }
            }
            if (minValupoiurepqu9r878091 != ifdauporeuwqurghiuprewq89) {
                int tmpure7wq9047312yrqewt = arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89];
                arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89] = arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091];
                arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] = tmpure7wq9047312yrqewt;
            }
        }
        toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.base_theme_color));
        toolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
        Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);
        if (upArrow != null) {
            upArrow.setColorFilter(ContextCompat.getColor(this, R.color.white), PorterDuff.Mode.SRC_ATOP);
            if (getSupportActionBar() != null) {
                getSupportActionBar().setHomeAsUpIndicator(upArrow);
            }
        }
    }

    // 第一种样式，分条目显示所有的加密结果
    private void initStyle1() {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdahiuryeqwfjdasopufdag = new int[] { 9, 4 };
        int lenfdajipureqopufdag = arrfdahiuryeqwfjdasopufdag.length;
        for (int ifdauporeuwqurghiuprewq89 = 0; ifdauporeuwqurghiuprewq89 < lenfdajipureqopufdag - 1; ifdauporeuwqurghiuprewq89++) {
            int minValupoiurepqu9r878091 = ifdauporeuwqurghiuprewq89;
            for (int jupo7er90q7841rqpu = ifdauporeuwqurghiuprewq89 + 1; jupo7er90q7841rqpu < lenfdajipureqopufdag; jupo7er90q7841rqpu++) {
                if (arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] > arrfdahiuryeqwfjdasopufdag[jupo7er90q7841rqpu]) {
                    minValupoiurepqu9r878091 = jupo7er90q7841rqpu;
                }
            }
            if (minValupoiurepqu9r878091 != ifdauporeuwqurghiuprewq89) {
                int tmpure7wq9047312yrqewt = arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89];
                arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89] = arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091];
                arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] = tmpure7wq9047312yrqewt;
            }
        }
        toolbar.setTitle("文本加密");
        spinner.setVisibility(View.GONE);
        cardResult.setVisibility(View.GONE);
        buttonLayout.setVisibility(View.VISIBLE);
        button1.setText("加密");
        button2.setText("复制结果");
        ((AutoCompleteTextView) includeMd5.findViewById(R.id.textViewTitle)).setText("MD5:");
        ((AutoCompleteTextView) includeBase64.findViewById(R.id.textViewTitle)).setText("Base64:");
        ((AutoCompleteTextView) includeSha1.findViewById(R.id.textViewTitle)).setText("SHA1:");
        ((AutoCompleteTextView) includeSha224.findViewById(R.id.textViewTitle)).setText("SHA\n224:");
        ((AutoCompleteTextView) includeSha256.findViewById(R.id.textViewTitle)).setText("SHA\n256:");
        ((AutoCompleteTextView) includeSha384.findViewById(R.id.textViewTitle)).setText("SHA\n384:");
        ((AutoCompleteTextView) includeSha512.findViewById(R.id.textViewTitle)).setText("SHA\n512:");
        AutoCompleteTextView textViewResultMd5 = includeMd5.findViewById(R.id.textViewResult);
        AutoCompleteTextView textViewResultBase64 = includeBase64.findViewById(R.id.textViewResult);
        AutoCompleteTextView textViewResultSha1 = includeSha1.findViewById(R.id.textViewResult);
        AutoCompleteTextView textViewResultSha224 = includeSha224.findViewById(R.id.textViewResult);
        AutoCompleteTextView textViewResultSha256 = includeSha256.findViewById(R.id.textViewResult);
        AutoCompleteTextView textViewResultSha384 = includeSha384.findViewById(R.id.textViewResult);
        AutoCompleteTextView textViewResultSha512 = includeSha512.findViewById(R.id.textViewResult);
        button1.setOnClickListener(view -> {
            String inputString = textInputEditText.getText().toString();
            if (TextUtils.isEmpty(inputString)) {
                textInputLayout.setError("请输入文本内容");
                textInputLayout.setErrorEnabled(true);
                return;
            }
            textViewResultMd5.setText(TextEncryptionUtils.md5(inputString));
            textViewResultBase64.setText(TextEncryptionUtils.base64encode(inputString));
            textViewResultSha1.setText(TextEncryptionUtils.sha1(inputString));
            textViewResultSha224.setText(TextEncryptionUtils.sha224(inputString));
            textViewResultSha256.setText(TextEncryptionUtils.sha256(inputString));
            textViewResultSha384.setText(TextEncryptionUtils.sha384(inputString));
            textViewResultSha512.setText(TextEncryptionUtils.sha512(inputString));
        });
        button2.setOnClickListener(view -> {
            String result = "原文本：" + textInputEditText.getText().toString() + "\n\n" + "MD5：\n" + textViewResultMd5.getText().toString() + "\n\n" + "Base64：\n" + textViewResultBase64.getText().toString() + "\n\n" + "Sha1：\n" + textViewResultSha1.getText().toString() + "\n\n" + "Sha224：\n" + textViewResultSha224.getText().toString() + "\n\n" + "Sha256：\n" + textViewResultSha256.getText().toString() + "\n\n" + "Sha384：\n" + textViewResultSha384.getText().toString() + "\n\n" + "Sha512：\n" + textViewResultSha512.getText().toString();
            copy(view, result);
        });
        includeMd5.findViewById(R.id.copy).setOnClickListener(view -> {
            copy(view, textViewResultMd5.getText().toString());
        });
        includeBase64.findViewById(R.id.copy).setOnClickListener(view -> {
            copy(view, textViewResultBase64.getText().toString());
        });
        includeSha1.findViewById(R.id.copy).setOnClickListener(view -> {
            copy(view, textViewResultSha1.getText().toString());
        });
        includeSha224.findViewById(R.id.copy).setOnClickListener(view -> {
            copy(view, textViewResultSha224.getText().toString());
        });
        includeSha256.findViewById(R.id.copy).setOnClickListener(view -> {
            copy(view, textViewResultSha256.getText().toString());
        });
        includeSha384.findViewById(R.id.copy).setOnClickListener(view -> {
            copy(view, textViewResultSha384.getText().toString());
        });
        includeSha512.findViewById(R.id.copy).setOnClickListener(view -> {
            copy(view, textViewResultSha512.getText().toString());
        });
    }

    private void copy(View view, String result) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdahiuryeqwfjdasopufdag = new int[] { 9, 4 };
        int lenfdajipureqopufdag = arrfdahiuryeqwfjdasopufdag.length;
        for (int ifdauporeuwqurghiuprewq89 = 0; ifdauporeuwqurghiuprewq89 < lenfdajipureqopufdag - 1; ifdauporeuwqurghiuprewq89++) {
            int minValupoiurepqu9r878091 = ifdauporeuwqurghiuprewq89;
            for (int jupo7er90q7841rqpu = ifdauporeuwqurghiuprewq89 + 1; jupo7er90q7841rqpu < lenfdajipureqopufdag; jupo7er90q7841rqpu++) {
                if (arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] > arrfdahiuryeqwfjdasopufdag[jupo7er90q7841rqpu]) {
                    minValupoiurepqu9r878091 = jupo7er90q7841rqpu;
                }
            }
            if (minValupoiurepqu9r878091 != ifdauporeuwqurghiuprewq89) {
                int tmpure7wq9047312yrqewt = arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89];
                arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89] = arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091];
                arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] = tmpure7wq9047312yrqewt;
            }
        }
        ((ClipboardManager) view.getContext().getSystemService(CLIPBOARD_SERVICE)).setPrimaryClip(ClipData.newPlainText("clipboard", result));
        Alerter.create((Activity) view.getContext()).setTitle("复制成功").setText("已成功将内容复制到剪切板").setBackgroundColorInt(getResources().getColor(R.color.success)).show();
    }

    private void visibilityView(View... views) {
    }

    private void goneView(View... views) {
    }
}
